﻿Imports DotNetNuke.Services.Social.Messaging
Imports DotNetNuke.Services.Social.Notifications
Imports DotNetNuke.Security.Roles
Imports DotNetNuke.Entities.Modules
Imports System.Linq

Namespace Components.Integration
    Public Class Notifications
        Public Const ContentTypeName As String = "RTP_HTMLNewsletter"
        Public Const NotificationNewNewsletterTypeName As String = "NewNewsletter"

        Public Sub NewNewsletterNotification(ByVal SendToRoles() As String, ByVal SenderUser As UserInfo, ByVal Subject As String, ByVal itemID As Integer, ByVal PortalID As Integer, ByVal displayTabModule As Integer, ByVal moduleid As Integer)
            Dim LocalResourceFile As String = "/DesktopModules/HTMLNewsLetter/App_LocalResources/Notifications"
            Dim notificationType As NotificationType = NotificationsController.Instance.GetNotificationType(NotificationNewNewsletterTypeName)
            If notificationType Is Nothing Then
                notificationType = CreateHTMLNewsletterNotificationTypes(PortalID)
            End If

            Dim objModuleController As New ModuleController
            Dim objModule As ModuleInfo = objModuleController.GetModule(moduleid)

            Dim notificationKey As String = String.Format("{0}:{1}:{2}:{3}", ContentTypeName + NotificationNewNewsletterTypeName, objModule.TabID, moduleid, itemID)

            Dim objNotification As New Notification
            objNotification.NotificationTypeID = notificationType.NotificationTypeId
            objNotification.Subject = Subject
            objNotification.Body = String.Format(Localization.GetString("NewHTMLNewsletterNotification", LocalResourceFile), ViewNewsletter(itemID, displayTabModule, objModule.TabID))
            objNotification.IncludeDismissAction = True
            objNotification.SenderUserID = SenderUser.UserID
            objNotification.Context = notificationKey
            objNotification.From = SenderUser.DisplayName

            Dim colRoles As New Generic.List(Of RoleInfo)
            Dim objRoleController As New RoleController
            For Each RoleName As String In SendToRoles
                Dim objRole As RoleInfo = objRoleController.GetRoleByName(PortalID, RoleName)
                colRoles.Add(objRole)
            Next

            Dim objScheduleItem As New DotNetNuke.Services.Scheduling.ScheduleItem
            objScheduleItem = DotNetNuke.Services.Scheduling.SchedulingProvider.Instance().GetSchedule("DotNetNuke.Services.Social.Messaging.Scheduler.CoreMessagingScheduler, DotNetNuke", Nothing)
            Dim scheduleEnabled As Boolean = objScheduleItem.Enabled
            Try
                If scheduleEnabled Then
                    objScheduleItem.Enabled = False
                    DotNetNuke.Services.Scheduling.SchedulingProvider.Instance().UpdateSchedule(objScheduleItem)
                End If

                NotificationsController.Instance.SendNotification(objNotification, PortalID, colRoles, Nothing)
                Dim messagingRecipients As Generic.IList(Of MessageRecipient) = DotNetNuke.Services.Social.Messaging.Internal.InternalMessagingController.Instance.GetMessageRecipients(objNotification.NotificationID)

                For Each objMessageRecipient As MessageRecipient In messagingRecipients
                    DotNetNuke.Services.Social.Messaging.Internal.InternalMessagingController.Instance.MarkMessageAsDispatched(objMessageRecipient.MessageID, objMessageRecipient.RecipientID)
                Next
            Catch
            End Try
            If scheduleEnabled Then
                objScheduleItem.Enabled = True
                DotNetNuke.Services.Scheduling.SchedulingProvider.Instance().UpdateSchedule(objScheduleItem)
            End If

        End Sub

        Private Function CreateHTMLNewsletterNotificationTypes(ByVal PortalID As Integer) As NotificationType
            Dim notificationType As New NotificationType
            Dim deskModuleId As Integer = DotNetNuke.Entities.Modules.DesktopModuleController.GetDesktopModuleByModuleName("HTMLNewsletter", PortalID).DesktopModuleID
            Dim objNotificationType As NotificationType = New NotificationType
            objNotificationType.Name = NotificationNewNewsletterTypeName
            objNotificationType.Description = "New HTML Newsletter"
            objNotificationType.DesktopModuleId = deskModuleId
            NotificationsController.Instance.CreateNotificationType(objNotificationType)

            Return objNotificationType

        End Function

        Public Function ViewNewsletter(ByVal NewsletterID As Integer, ByVal displayTabModule As Integer, ByVal currentTabid As Integer) As String
            Dim objModuleController As New ModuleController
            Dim TabModule As ModuleInfo = objModuleController.GetTabModule(displayTabModule)
            Dim tabid As Integer = currentTabid
            If Not TabModule Is Nothing Then
                tabid = TabModule.TabID
            End If
            Dim url As String = NavigateURL(tabid)
            Dim addPar As String = "?"
            If InStr(url, "?") > 0 Then
                addPar = "&"
            End If
            url = url & addPar & "ViewID=" & NewsletterID.ToString & "&Page=1"
            Return url
        End Function

    End Class
End Namespace
